//
//  BaseViewController.m
//  MicroCaf
//
//  Created by 张力川 on 16/2/29.
//  Copyright © 2016年 TOOCMS. All rights reserved.
//
#import "BaseViewController.h"

@interface BaseViewController ()
@property(nonatomic,copy)NSString *badcount;

@end
@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Graycolor;
    self.automaticallyAdjustsScrollViewInsets =NO;
    
  //购物车接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CartbadageClick) name:CartbadgeValueNotification object:nil];
}
- (void)CartbadageClick
{
    
    [self cartNum];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
     [self.view endEditing:NO];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [self.view endEditing:NO];
    //导航控制器第一层视图
//    if (self == self.navigationController.childViewControllers[0]) {
//
//        if ([self class] == [TSShoppingCartVC class]) {
//            self.tabBarController.childViewControllers[2].tabBarItem.badgeValue = nil;
//        }else{
//            if ([User userIsLogin]) {
//                [self cartNum];
//            }else{
//                self.tabBarController.childViewControllers[2].tabBarItem.badgeValue = nil;
//
//            }
//        }
//    }
}



#pragma mark 打印相关
- (void)dealloc {
    TSLog2(@"%@ Released",self);
    
}

- (NSString *)description {
    return NSStringFromClass([self class]);
}

#pragma mark 网络请求
-(void)cartNum{
    NSString *urlStr = @"Cart/getTotalQuantity";
    TSParamter *paramter = [TSParamter paramterWithMD5];
    paramter.m_id = userID;
    [TSNetworking POSTWithURL:urlStr paramsModel:paramter completeBlock:^(id request) {
        if ([request[@"flag"] isEqualToString:@"success"]) {
            TSLog(request);
            
            self.badcount = request[@"data"][@"quantity_total"];
            [[NSUserDefaults standardUserDefaults]setValue:self.badcount forKey:@"badcount"];

            
            NSString *page_type= request[@"data"][@"page_type"];
            [[NSUserDefaults standardUserDefaults]setValue:page_type forKey:@"pagetype"];
            
            
         if ([self.badcount isEqualToString:@"0"]) {
                self.tabBarController.childViewControllers[2].tabBarItem.badgeValue = nil;
            }else{
                self.tabBarController.childViewControllers[2].tabBarItem.badgeValue = self.badcount;
                
                if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_9_x_Max) {
                    [self.tabBarController.childViewControllers[2].tabBarItem setBadgeColor:ThemeColor];
                }
            }
            
        }else{
            
        }
    } failBlock:^(NSError *error) {
        
    }];
}

@end
